<?php
/**
 * SHOP 
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*会员列表*/
if($_REQUEST['act']=='list'){ 
	$user_rank = isset($_REQUEST['user_rank'])?$_REQUEST['user_rank']:0;//会员等级
	$user_name = isset($_REQUEST['user_name'])?$_REQUEST['user_name']:0;//会员名称
	$start_time = strtotime(isset($_REQUEST['start_time'])?$_REQUEST['start_time']:0);//注册开始时间
	$end_time = strtotime(isset($_REQUEST['end_time'])?$_REQUEST['end_time']:0);//注册结束时间
	//查询用户列表
	$sql = "select us.*,ra.rank_name from ".ly()."users us,".ly()."user_rank ra";
	$where=" where us.user_rank=ra.rank_id";
	//搜索用户信息
	if($user_rank){$where.=" and us.user_rank='{$user_rank}'";}
	if($user_name){$where.=" and user_name like '%{$user_name}%'";}
	if($start_time){$where.=" and us.reg_time >= '{$start_time}'";}
	if($end_time){$where.=" and us.reg_time < '{$end_time}'";}
    $order=" order by us.user_id desc";
    $sql.=$where.$order;
    $arr=array();
    $arr['user_rank']=$user_rank;
    $arr['user_name']=$user_name;
    $arr['start_time']=$start_time;
    $arr['end_time']=$end_time;
    $smarty->assign('sou',$arr);
	/*分页开始*/
	$num='10';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$row=$db->getAll($sql);
	$sqla="SELECT * FROM ".ly()."user_rank";
	$res=$db->getAll($sqla);
	$smarty->assign('user_ranks',$res);
	$smarty->assign('user',$row);
	$smarty->display('users_list.html');
}elseif($_REQUEST['act']=='edit'){
	@$user_id=$_REQUEST['user_id'];
	if($user_id>0){
		$sql="select * from ".ly()."users where user_id='{$user_id}'";
		$user_edit=$db->getRow($sql);	
		$smarty->assign('user_edit',$user_edit);
	}
	$sqlrank="select * from ".ly()."user_rank";
	$user_rank=$db->getAll($sqlrank);
	$smarty->assign('user_rank',$user_rank);
	$smarty->display('users_edit.html');	
}elseif($_REQUEST['act']=='insert'){
	$user_id = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:0;//会员ID
	$user_name = isset($_REQUEST['user_name'])?$_REQUEST['user_name']:0;//会员账号
	$pass = isset($_REQUEST['password'])?$_REQUEST['password']:0;//会员密码
	$repass = isset($_REQUEST['repassword'])?$_REQUEST['repassword']:0;//会员密码
	$user_rank = isset($_REQUEST['user_rank'])?$_REQUEST['user_rank']:0;//会员等级
	$alias = isset($_REQUEST['alias'])?$_REQUEST['alias']:0;//会员昵称
	$sex = isset($_REQUEST['sex'])?$_REQUEST['sex']:0;//会员性别
	$email = isset($_REQUEST['email'])?$_REQUEST['email']:0;//会员邮箱
	$head_photo="uploadfile/image/20170721/20170721155259_59861.png";//默认头像
	$password=md5($pass);
	if($user_id>0){
		$sql="update ".ly()."users set user_name='{$user_name}',password='{$password}',user_rank='{$user_rank}',alias='{$alias}',sex='{$sex}',email='{$email}' where user_id='{$user_id}'";	
	}else{
		$sql="insert into ".ly()."users(user_name,password,user_rank,alias,sex,email,reg_time,head_photo) values('{$user_name}','{$password}','{$user_rank}','{$alias}','{$sex}','{$email}','".time()."','{$head_photo}')";
	}
	$res=$db->query($sql);
	if($res){
		 $href[]=array('text'=>"返回会员列表",'href'=>'users.php?act=list');
         sys_msg("操作成功！",$href);
	}else{
		$href[]=array('text'=>"重新编辑",'href'=>'history.back()');
        sys_msg("操作失败！",$href,false);
	}
}elseif($_POST['act']=='user_name'){
	$user_name = isset($_POST['user_name'])?$_POST['user_name']:0;//会员账号
	$sql="select user_id from ".ly()."users where user_name='{$user_name}'";
	$res=$db->getOne($sql);
	if($res){
		echo 1;
	}else{
		echo 2;
	}
}elseif($_REQUEST['act']=='disable'){
	$user_id=isset($_POST['id'])?$_POST['id']:0;
	$vals=isset($_POST['val'])?$_POST['val']:0;
	if($vals==1){$zt='1';}else{$zt='0';}
	$sql="update ".ly()."users set disable='{$zt}' where user_id='{$user_id}'";
	$res=$db->query($sql);
	if($res){
		echo "1";	
	}else{
		echo "2";
	}
	
}